﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;

namespace SaiPuLSCoffee.model
{
    public class Robot2Msg
    {
        private static Robot2Msg instance;
        private Robot2Msg() { }

        public static Robot2Msg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Robot2Msg();
                }
                return instance;
            }
        }
        #region IO通讯
        public bool AllBol = true;
        public bool RobotBol = true;
        public delegate void IOdelegate(string btnName, string tag);
        public IOdelegate iodelegate;
        /// <summary>
        ///  IO的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        public static bool IsMsgError = true;
        string nostrs = "";
        public Thread All_thr;
        public void IOSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.Robot2Name)))
                {
                    Thread.Sleep(1000);
                    IOSerial_Thr();
                }
                Robot2SerialPort.Instance.IceMachineConnect(IPorPortInfo.Instance.Robot2Name, 38400, "", 8, 1);//其他串口通讯
                Robot2SerialPort.Instance.serialPort.DataReceived += SerialPort_DataReceived;
            }
            catch (Exception)
            {
                Robot2SerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        public void RobotRQK(bool cqyd, int qb, int LeachInit)
        {
            try
            {
                Thread.Sleep(300);
                if (cqyd)
                {
                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotCQDYDR_Down);
                    Thread.Sleep(300);
                }
                if (qb == 1)
                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQB1R_Down);
                else
                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQB2R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJBR_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQCQZBR_Down);
                Thread.Sleep(300);
                if (LeachInit == 1)
                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW1CQR_Down);
                else
                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW2CQR_Down);
            }
            catch (Exception)
            {

            }
        }
        public void RobotQKALL()
        {
            try
            {
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP1R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP2R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP3R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP4R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQB1R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQB2R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJBR_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQCQZBR_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW1CQR_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW2CQR_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB1R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB2R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJB1R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJB2R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotPCQDJB12R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotPCQDJB3R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotCQDYDR_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJBWCR_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW1QBR_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW2CQR_Down);
            }
            catch (Exception)
            {
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] result = new byte[Robot2SerialPort.Instance.serialPort.BytesToRead];
                Robot2SerialPort.Instance.serialPort.Read(result, 0, Robot2SerialPort.Instance.serialPort.BytesToRead);
                buffer.AddRange(result);
                string str = "";// $"{DateTime.Now}：\n";
                                //判断读写模式
                if (buffer.Count > 0x03)
                    str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                //Console.WriteLine(str);
                MsgShow(str);
                Thread.Sleep(1);
            }
            catch (Exception)
            {
                return;
            }
        }
        string zblist = "";
        public void MsgShow(string str)
        {
            try
            {
                var nostr = str.Split(' ');
                nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                if (nostr.Count() > 1)
                {
                    for (int i = 1; i < nostr.Count() - 1; i++)
                    {
                        nostrs += nostr[i] + " ";
                    }
                }
                if (nostrs.Length > 12)
                {
                    if (nostrs.Substring(0, 5) == "04 02")
                    {
                        Console.WriteLine(nostrs);
                        var robstr = nostrs.Split(' ');
                        RobotBol = robstr[3] == "01" ? true : false;
                        Console.WriteLine(robstr[3] == "01" ? true : false);
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        #endregion
    }
}
